package com.eric.demo.alibishi;

import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * @author guangyin.zhang
 * @since 2020/4/2
 *
 * 给定一个4G的文件，每行都是一个long长整数，在可用内存为1G的情况下，要求对文件内容做排序（升序）
 */
public class SortTest {

    static String path = "D://sortFile/file_";

    public static void main(String[] args) throws IOException {
        File file = new File("");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line;
        List<Long> dataList = Lists.newArrayList();
        int length = 1024 * 1024 / 8;
        int k = 0;
        int fileindex = 0;
        while ((line = bufferedReader.readLine()) != null){
            if(k < length){
                dataList.add(Long.valueOf(line));
                k++;
            }else{
                Collections.sort(dataList);
                writeFile(dataList, fileindex ++);
                dataList.clear();
                k = 0;
            }
        }
        bufferedReader.close();

        //接下来对path目录下的文件进行归并排序
        file.delete();
        file.createNewFile();
        sort(file);

    }


    static void writeFile(List<Long> dataList, int fileindex) throws IOException {
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(new File(path + fileindex)));
            for(Long data : dataList){
                writer.newLine();
                writer.write(data.toString());
            }
        }finally {
            if(writer != null){
                writer.close();
            }
        }
    }


    static void sort(File file) throws IOException {
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader(path + 1));
        BufferedReader bufferedReader2 = new BufferedReader(new FileReader(path + 2));
        BufferedReader bufferedReader3 = new BufferedReader(new FileReader(path + 3));
        BufferedReader bufferedReader4 = new BufferedReader(new FileReader(path + 4));

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        // 找出最小值写入文件，继续读取下一行数据
        Long data1 = Long.valueOf(bufferedReader1.readLine());
        Long data2 = Long.valueOf(bufferedReader2.readLine());
        Long data3 = Long.valueOf(bufferedReader3.readLine());
        Long data4 = Long.valueOf(bufferedReader4.readLine());
        Long[] datas = new Long[]{data1, data2, data3, data4};

        int index = findMinIndex(datas);
        while (true){
        }
    }

    static int findMinIndex(Long[] datas){
        int index = 0;
        Long minData = datas[0];
        for(int i=1; i< datas.length; i++){
            if(datas[i] < minData){
                index = 2;
                minData = datas[i];
            }

        }
        return index;
    }


}
